<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp" %>
<%@ include file="/common/grid/jqGrid.jsp" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title></title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="">
	<meta http-equiv="description" content="This is my page">

	<base target="_self" />
  </head>
  
  <body>
    <center>
    	<s:form name="frm" id="frm" method="post" action="" namespace="">
    		<jsp:include page="/jsp/outward/business/common/menuLink.jsp">
    			<jsp:param name="menuLink" value="[{'name':'新增码表'}]"/>
    			<jsp:param name="width" value="50%"/>
    		</jsp:include>
    		
    		<s:hidden name="ct.id" id="ctId"></s:hidden>
    		<s:hidden name="ct.ctType" id="ctType"></s:hidden>
    		<s:hidden name="keyArray" id="keyArrayHidden"></s:hidden>
    		<s:hidden name="labelArray" id="labelArrayHidden"></s:hidden>
    		
    		<table cellspacing="1" cellpadding="3" bgcolor="#CCCCCC" width="100%">
    			<tr bgcolor="#FFFFFF">
    				<td width="20%" class="label" align="right">
    					编码：
    				</td>
    				<td width="30%" align="left">
   						<s:textfield name="ct.code" id="code" cssStyle="width:100px;" maxlength="10"></s:textfield>
    				</td>
    				<td width="20%" class="label" align="right">
    					类型：
    				</td>
    				<td width="30%" align="left">
    					<s:if test='@com.pk.platform.util.constant.QuestionConstant@CT_TYPE_CODE_TABLE.equals(ct.ctType)'>
   						码表
    					</s:if>
    					<s:else>
    					组题
    					</s:else>
    				</td>
    			</tr>
    			<tr bgcolor="#FFFFFF">
    				<td class="label" align="right">
    					描述：
    				</td>
    				<td align="left" colspan="3">
    					<s:textarea name="ct.description" id="description" cssStyle="width:95%; height:80px;"></s:textarea>
    				</td>
    			</tr>
    			<tr bgcolor="#FFFFFF" id="halfQesTr">
    				<td class="label" align="right">
    					是否半开放题：
    				</td>
    				<td align="left">
   						<input type="checkbox" value="Y" name="ct.hasHalfQes" id="hasHalfQes" onclick="halfQesClick()" <s:if test='"Y".equals(ct.hasHalfQes)'>checked</s:if> />
    				</td>
    				<td class="label" align="right" width="20%">
    					半开放题值：
    				</td>
    				<td align="left">
    					<s:textfield name="ct.halfQesVal" id="halfQesVal" cssStyle="width:80px;" maxlength="5" readonly="true"></s:textfield>
    				</td>
    			</tr>
    			<tr bgcolor="#FFFFFF" id="halfQesTr">
    				<td class="label" align="right">
    					半开放题宽度：
    				</td>
    				<td align="left">
   						<s:select list="commonList" listKey="value" listValue="label" cssStyle="width:90px;" name="ct.halfQesWidth" id="halfQesWidth"></s:select>
    				</td>
    				<td class="label" align="right" width="20%">
    					半开放题个数：
    				</td>
    				<td align="left">
    					<s:select list="commonList" listKey="value" listValue="label" cssStyle="width:90px;" name="ct.halfQesQuantity" id="halfQesQuantity"></s:select>
    				</td>
    			</tr>
    		</table>
    		
    		<table width="100%" cellspacing="0">
    			<tr>
    				<td align="center" valign="bottom" height="30">
    					<input type="button" value="保存" class="btn" onclick="saveThis()" />
    				</td>
    			</tr>
    		</table>
    		
    		<table cellspacing="1" cellpadding="3" bgcolor="#CCCCCC" width="100%">
    			<tr bgcolor="#FFFFFF">
    				<td id="listTd" width="50%" align="left" height="200px">
    					
    					<input type="button" value="删除" class="btn" onclick="delItemOption()" />
    					<table id="list" cellpadding="3" cellspacing="0"></table>
    					
    				</td>
    				<td width="50%" align="center">
    					<table width="100%">
    						<tr>
    							<td align="left" width="15%">
    								值在：
    							</td>
    							<td align="left" width="85%">
    								<s:radio list='#{"前":"前", "后":"后"}' value="'前'" id="valuePosition" name="valuePosition"></s:radio>
    							</td>
    						</tr>
    						<tr style="color:red;">
    							<td align="left">
    								例：
    							</td>
    							<td align="left">
    								1&nbsp;&nbsp;&nbsp;男<br>
    								2&nbsp;&nbsp;&nbsp;女
    							</td>
    						</tr>
    						<tr>
    							<td align="center" colspan="2">
    								<s:textarea id="itemOptionArea" cssStyle="width:95%; height:150px;"></s:textarea>
    							</td>
    						</tr>
    						<tr>
    							<td align="center" colspan="2">
			   						<input type="button" value="新增" class="btn" onclick="addItemOption()" />
    							</td>
    						</tr>
    					</table>
   						
    				</td>
    			</tr>
    		</table>
    	</s:form>
    </center>
    
    <script type="text/javascript">
    $(function(){
    	halfQesClick();
    	ctTypeChange();
    	itemOptionListInit();
    	
    	$(window).resize(function(){
        	gridAutoWidth('list', 'listTd');
        });
    });
    
    function saveThis()
    {
    	var code = $('#code');
    	var description = $('#description');
    	var hasHalfQes = $('#hasHalfQes');
    	var halfQesVal = $('#halfQesVal');
    	if(!isNotNull(code.val()))
    	{
    		showMsg('编码不能为空。', function(){code.focus();});
    		return;
    	}
    	if(description.val().length > 200)
    	{
    		showMsg('描述不能超过200字。', function(){description.focus();});
    		return;
    	}
    	if(!isNotNull(halfQesVal.val()) && hasHalfQes.attr('checked'))
    	{
    		showMsg('请填写半开放题值。', function(){halfQesVal.focus();});
    		return;
    	}
    	
    	ajaxAction('inner/qes/updateCodeTable.action', 'post', 'frm', true, function(obj){
    		if('hasCode' == obj.result)
    		{
    			showMsg('已存在该码表编码。', function(){
	   				code.focus();
	   			});
    		}
    		else
    		{
	   			showMsg('保存成功。', function(){
	   				var pWin = getParentWin();
    				var codeTableId = pWin.$('#codeTableId');
    				var groupQes = pWin.$('#groupQes');
    				var initValue = codeTableId.val();
    				var groupQesVal = groupQes.val();
    				if('codeTable' == obj.ctType)
    				{
	    				addSelectOption2(codeTableId, obj.ctList, 'id', 'code', true);
	    				if(isNotNull(initValue))
	    				{
	    					codeTableId.val(initValue);
	    				}
    				}
    				else if('groupQes' == obj.ctType)
    				{
	    				addSelectOption2(groupQes, obj.ctList, 'id', 'code', true);
	    				if(isNotNull(groupQesVal))
	    				{
	    					groupQes.val(groupQesVal);
	    				}
    				}
	   				window.close();
	   			});
	   		}
    	});
    }
    
    function halfQesClick()
    {
    	var hasHalfQes = $('#hasHalfQes');
    	var halfQesVal = $('#halfQesVal');
    	var halfQesWidth = $('#halfQesWidth');
    	var halfQesQuantity = $('#halfQesQuantity');
    	if(hasHalfQes.attr('checked'))
    	{
    		halfQesVal.attr('readonly', false);
    		halfQesWidth.attr('disabled', false);
    		halfQesQuantity.attr('disabled', false);
    	}
    	else
    	{
    		halfQesVal.val('');
    		halfQesVal.attr('readonly', true);
    		halfQesWidth.attr('disabled', true);
    		halfQesQuantity.attr('disabled', true);
    	}
    }
    
    function ctTypeChange()
    {
    	var ctType = $('#ctType');
    	var halfQesTr = $('#halfQesTr');
    	if('codeTable' == ctType.val())
    	{
    		halfQesTr.css('display', 'block');
    	}
    	else if('groupQes' == ctType.val())
    	{
    		halfQesTr.css('display', 'none');
    		var hasHalfQes = $('#hasHalfQes');
    		var halfQesVal = $('#halfQesVal');
    		hasHalfQes.attr('checked', false);
    		halfQesClick();
    	}
    }

    function delItemOption()
    {
    	if(!hasCheck('jqglist'))
    	{
    		return;
    	}
    	showConfirm('确认删除？', function(v){
    		if(v == true)
    		{
    			ajaxAction('inner/qes/delItemOption.action', 'post', 'frm', false, function(){
					toSearchList('list');
				});
    		}
    	});
    }

    function addItemOption()
    {
		var valuePosition = $('input:radio[name="valuePosition"][checked]');
		var itemOptionArea = $('#itemOptionArea');
		var ctType = $('#ctType');
		var itemOptionValue = itemOptionArea.val();
		var values = itemOptionValue.split('\n');
		var len = values.length;
		var keyArray = new Array();
		var labelArray = new Array();
		if(!isNotNull(itemOptionArea.val()))
		{
			showMsg('选项不能为空。', function(){itemOptionArea.focus();});
			return;
		}
		for(var i = 0; i < len; i++)
		{
			var val = values[i];
			if(!isNotNull(val))
			{
				continue;
			}
			len2 = val.length;
			var result = '';
			var x = 1;
			for(var j = 0; j < len2; j++)
			{
				if(' ' != val.charAt(j) && '.' != val.charAt(j))
				{
					result += val.charAt(j);
				}
				else if(x == 1)
				{
					result += ' ';
					x++;
				}
			}
			var keyLabel = result.split(' ');
			if(keyLabel.length == 2)
			{
				var ptn = /^[0-9]*$/;
				var ptn2 = /^[a-zA-Z]*$/;
				if('前' == valuePosition.val())
				{
					if('codeTable' == ctType.val() && !ptn.exec(keyLabel[0].Trim()))
					{
						showMsg('值有误，只能为数字。');
						return;
					}
					if('groupQes' == ctType.val() && !ptn.exec(keyLabel[0].Trim()) && !ptn2.exec(keyLabel[0].Trim()))
					{
						showMsg('值有误，只能为数字或字母。');
						return;
					}
					if(keyArrayExist(keyArray, keyLabel[0].Trim()))
					{
						return;
					}
					if(keyLabel[0].Trim().length > 5)
					{
						showMsg('值不能超过5字。');
						return;
					}
					if(keyLabel[1].length > 150)
					{
						showMsg('描述不能超过150字。');
						return;
					}
					keyArray.push(keyLabel[0].Trim());
					labelArray.push(keyLabel[1]);
				}
				else
				{
					if('codeTable' == ctType.val() && !ptn.exec(keyLabel[1].Trim()))
					{
						showMsg('值有误，只能为数字。');
						return;
					}
					if('groupQes' == ctType.val() && !ptn.exec(keyLabel[1].Trim()) && !ptn2.exec(keyLabel[1].Trim()))
					{
						showMsg('值有误，只能为数字或字母。');
						return;
					}
					if(keyArrayExist(keyArray, keyLabel[1].Trim()))
					{
						return;
					}
					if(keyLabel[1].Trim().length > 5)
					{
						showMsg('值不能超过5字。');
						return;
					}
					if(keyLabel[0].length > 150)
					{
						showMsg('描述不能超过150字。');
						return;
					}
					keyArray.push(keyLabel[1].Trim());
					labelArray.push(keyLabel[0]);
				}
			}
			else
			{
				showMsg('选项有误。');
				return;
			}
		}
		var repeatFlag = checkRepeat(keyArray);
		if(repeatFlag == true)
		{
			return;
		}
		setKeyLabelValue(keyArray, labelArray);
		ajaxAction('inner/qes/addItemOption.action', 'post', 'frm', false, function(){
			toSearchList('list');
			itemOptionArea.val('');
		});
    }

    function itemOptionListInit()
    {
    	var ctType = $('#ctType');
    	var isHide = 'codeTable' == ctType.val() ? true : false;
    	var cName = new Array('值','选项', '操作');
    	var cModel = [
    					{name:'optionValue', index:'optionValue', width:'20%', align:'center', sortable:false, formatter:optionValueFormat},  
                        {name:'description', index:'description', width:'60%', align:'center', sortable:false},
                        {name:'id', index:'id', width:'20%', align:'center', sortable:false, formatter:optionFormat, hidden:isHide}
                      ];
        
        newJqGrid({
        	id: 'id',
        	gridId: 'list',
        	url: 'inner/qes/itemOptionList.action?ct.id=' + $('#ctId').val(),
        	head: cName,
        	column: cModel,
        	width: $('#listTd').width() - 5,
        	height: 175,
        	hasCb: true,
        	title: '选项列表'
        });
    }

    function optionValueFormat(ov)
    {
		return ov + '<input type="hidden" name="ovHidden" value="' + ov + '" />';
    }

    function optionFormat(ov)
    {
		return '<a href="javascript:updateItemOption(\'' + ov + '\')">修改</a>';
    }
    
    function updateItemOption(v)
    {
    	var url = realPath + '/inner/qes/toUpdateItemOption.action?io.id=' + v;
    	showMd(url, 700, 550, 'yes', 'yes');
    }

    function checkRepeat(keyArray)
    {
        var ovHidden = $('input:hidden[name="ovHidden"]');
        var len = keyArray.length;
        var len2 = ovHidden.length;
        for(var i = 0; i < len; i++)
        {
        	for(var j = 0; j < len2; j++)
            {
                var ovh = $(ovHidden.get(j));
                if(ovh.val().Trim() == keyArray[i].Trim())
                {
                    showMsg('值' + ovh.val() + '重复');
					return true;
                }
            }
        }
		return false;
    }

    function setKeyLabelValue(keyArray, labelArray)
    {
        var sp = '#split#';
        var len = keyArray.length;
        var keyArrayHidden = $('#keyArrayHidden');
        var labelArrayHidden = $('#labelArrayHidden');
        keyArrayHidden.val('');
        labelArrayHidden.val('');
        for(var i = 0; i < len; i++)
        {
        	keyArrayHidden.val(keyArrayHidden.val() + keyArray[i]);
        	labelArrayHidden.val(labelArrayHidden.val() + labelArray[i]);
            if(i != len - 1)
            {
            	keyArrayHidden.val(keyArrayHidden.val() + sp);
            	labelArrayHidden.val(labelArrayHidden.val() + sp);
            }
        }
    }
    
    function keyArrayExist(arr, val)
    {
    	var len = arr.length;
    	for(var i = 0; i < len; i++)
    	{
    		if(arr[i] == val)
    		{
    			showMsg('值' + arr[i] + '重复');
    			return true;
    		}
    	}
    	return false;
    }
    </script>
  </body>
</html>
